<?php

namespace app\admin\service;

use app\admin\model\BoxAdMedia;
use app\admin\model\BoxDataOverview;
use app\admin\model\BoxGame;
use app\admin\model\BoxMemberRemainLog;
use app\admin\model\BoxStatisticsAdGameTrackLog;
use app\admin\model\BoxStatisticsAdOverviewLog;
use app\admin\model\Package;
use app\admin\model\UserPromote;

class BoxStatisticsAdGameTrackService extends BaseService
{

    /**
     * @Notes:盒子投放游戏数据总览
     * @Interface getList
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }

        if (isset($param['game_id']) && !empty($param['game_id'])) {
            $map[] = ['box_game_id', '=', $param['game_id']];
        }

        $list = (new BoxStatisticsAdGameTrackLog())
            ->field('day_time,platform,promote_id,box_game_id,package_id,media_id,sum(runing_water) as runing_water,sum(register_num) as register_num,sum(new_register_num) as new_register_num,sum(new_role_num) as new_role_num,sum(active_account_num) as active_account_num,sum(pay_num) as pay_num,sum(new_pay_num) as new_pay_num,sum(new_pay_amount) as new_pay_amount')
            ->where($map)
            ->order('day_time', 'desc');

        if (empty($param['game_id'])) {
            $list->group('day_time');
        } else {
            $list->group('day_time,box_game_id');
        }

        $list = $list->select()->toArray();
        //获取数据总数
        $count = count($list);

        //游戏
        $gameData = BoxGame::field(['id', 'game_name'])->select()->toArray();
        $gameData = array_column($gameData, 'game_name', 'id');

        //媒体
        $promoteData = UserPromote::field(['id', 'realname'])->select()->toArray();
        $promoteData = array_column($promoteData, 'realname', 'id');

        //产品
        $packageData            = Package::field(['id', 'package_name'])->select()->toArray();
        $packageData            = array_column($packageData, 'package_name', 'id');
        $packageData['9999997'] = '千年游戏盒子pc';
        $packageData['9999998'] = '千年游戏盒子ios';
        $packageData['9999999'] = '千年游戏盒子android';

        //投放渠道
        $mediaData = BoxAdMedia::field(['id', 'name'])->select()->toArray();
        $mediaData = array_column($mediaData, 'name', 'id');

        if (!empty($list)) {
            $runing_water_all       = 0;
            $register_num_all       = 0;
            $new_register_num_all   = 0;
            $role_rate_all          = 0;
            $new_role_num_all       = 0;
            $active_account_num_all = 0;
            $pay_num_all            = 0;
            $pay_rate_all           = 0;
            $arppu_all              = 0;
            $darpu_all              = 0;
            $new_pay_num_all        = 0;
            $new_pay_amount_all     = 0;
            $new_pay_rate_all       = 0;
            $new_arppu_all          = 0;
            foreach ($list as $key => &$item) {
                $item['day_time']     = date("Y-m-d", strtotime($item['day_time']));
                $item['media_name']   = empty($param['media_id']) ? '-' : $mediaData[$item['media_id']] ?? '';
                $item['promote_name'] = empty($param['promote_id']) ? '-' : $promoteData[$item['promote_id']] ?? '';
                $item['platform']     = empty($param['platform']) ? '-' : $item['platform'];
                $item['package_name'] = empty($param['platform']) ? '-' : $packageData[$item['package_id']] ?? '';
                $item['game_name']    = empty($param['game_id']) ? '全部游戏' : $gameData[$item['box_game_id']] ?? '';
                $item['role_rate']    = empty($item['register_num']) ? '0' : round($item['new_register_num'] / $item['register_num'] * 100);
                $item['pay_rate']     = empty($item['active_account_num']) ? '0' : round($item['pay_num'] / $item['active_account_num'] * 100);
                $item['arppu']        = empty($item['pay_num']) ? '0' : round($item['new_pay_amount'] / $item['pay_num'], 2);
                $item['darpu']        = empty($item['active_account_num']) ? '0' : round($item['new_pay_amount'] / $item['active_account_num'], 2);
                $item['new_pay_rate'] = empty($item['register_num']) ? '0' : round($item['new_pay_num'] / $item['register_num'] * 100);
                $item['new_arppu']    = empty($item['new_pay_num']) ? '0' : round($item['new_pay_amount'] / $item['new_pay_num'], 2);

                $runing_water_all       += $item['runing_water'];
                $register_num_all       += $item['register_num'];
                $new_register_num_all   += $item['new_register_num'];
                $role_rate_all          += $item['role_rate'];
                $new_role_num_all       += $item['new_role_num'];
                $active_account_num_all += $item['active_account_num'];
                $pay_num_all            += $item['pay_num'];
                $pay_rate_all           += $item['pay_rate'];
                $arppu_all              += $item['arppu'];
                $darpu_all              += $item['darpu'];
                $new_pay_num_all        += $item['new_pay_num'];
                $new_pay_amount_all     += $item['new_pay_amount'];
                $new_pay_rate_all       += $item['new_pay_rate'];
                $new_arppu_all          += $item['new_arppu'];

                $item['role_rate']    = $item['role_rate'] . "%";
                $item['pay_rate']     = $item['pay_rate'] . "%";
                $item['new_pay_rate'] = $item['new_pay_rate'] . "%";
            }

            $list[$count]['day_time']           = "汇总";
            $list[$count]['media_name']         = "-";
            $list[$count]['package_name']       = "-";
            $list[$count]['platform']           = "-";
            $list[$count]['promote_name']       = "-";
            $list[$count]['game_name']          = "-";
            $list[$count]['runing_water']       = $runing_water_all;
            $list[$count]['register_num']       = $register_num_all;
            $list[$count]['new_register_num']   = $new_register_num_all;
            $list[$count]['role_rate']          = round($role_rate_all / $count) . "%";
            $list[$count]['new_role_num']       = $new_role_num_all;
            $list[$count]['active_account_num'] = $active_account_num_all;
            $list[$count]['pay_num']            = $pay_num_all;
            $list[$count]['pay_rate']           = round($pay_rate_all / $count, 2) . "%";
            $list[$count]['arppu']              = round($arppu_all / $count, 2);
            $list[$count]['darpu']              = round($darpu_all / $count, 2);
            $list[$count]['new_pay_num']        = $new_pay_num_all;
            $list[$count]['new_pay_amount']     = $new_pay_amount_all;
            $list[$count]['new_pay_rate']       = round($new_pay_rate_all / $count, 2) . "%";
            $list[$count]['new_arppu']          = round($new_arppu_all / $count, 2);
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

}
